home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / which_mc.zip / WHICH.C < prev    next >
C/C++ Source or Header  |  1988-04-27  |  3KB  |  110 lines

  1. /* which.c - tell which program is executed when you type it's name */
  2. /*     usage: which command [command]... */
  3. /* fixes bug in earlier version using turbo-C searchpath() */
  4. /* should be completely portable now */
  5.  
  6. #include <stdio.h>
  7. #define lerror( str )      {  fputs( str, stderr ); _exit(1);  }
  8. #define EXIST(s) (modtime(s) !=0)
  9.  
  10. static char *h="$header: which.c ver 8 Matt Cohen size 2725 time 4/27/88 23:01:08$";
  11. static char *c="$copyright:  (c) 1988 Matt Cohen$";
  12.  
  13. main(argc,argv)
  14. int argc; char *argv[];
  15. char *searchpath(), buf[BUFSIZ], *pathname, *strcpy();
  16. int i,flag=0; long modtime();
  17.  
  18. if (argc < 2) lerror("usage: which command [command]...\n");
  19. for(i=1; i < argc; i++) {
  20.     flag=0;
  21.      strcpy(buf,argv[i]);
  22.     pathname=searchpath(buf);
  23.      flag=(pathname != NULL);
  24.     if (flag) printf("%s\n",pathname);
  25.    else printf("%s not found\n",argv[i]);
  26.    }
  27. }
  28.  
  29. /* make full filename fn have suffix s */
  30. suffix(fn,q)
  31. char fn[],q[];
  32. {    int k; int i;
  33.     for(k=0; k < strlen(fn) && fn[k] != '.'; k++);
  34.    if (k == strlen(fn)) fn[k]='.';
  35.    k++;
  36.    fn[k]='\0';
  37.    strcat(fn,q);
  38. }
  39.  
  40. /* search the path for the program s , looking first in . */
  41. char *searchpath(s)
  42. char *s ;
  43. {    char *B[20]; int i,n; char *getenv(), *path, *strsave(), *strcpy();
  44.     char f[50], dir[50];
  45.     long modtime(); 
  46.       /*  look in . first */
  47.       
  48.       suffix(s,"COM");
  49.         if (EXIST(s))  return(strsave(s));
  50.         suffix(s,"EXE");
  51.         if (EXIST(s))  return(strsave(s));
  52.         suffix(s,"BAT");
  53.         if (EXIST(s))  return(strsave(s));
  54.       /* now look through path */
  55.     if ((path=getenv("PATH"))==NULL) return(NULL); /* no path */
  56.         n=split(path,B);
  57.     for(i=0; i<n; i++)
  58.     {
  59.         strcpy(dir,B[i]); strcat(dir,"\\");
  60.         strcat(dir,s);
  61.       suffix(dir,"COM");
  62.         if (EXIST(dir))  return(strsave(dir));
  63.         suffix(dir,"EXE");
  64.         if (EXIST(dir))  return(strsave(dir));
  65.         suffix(dir,"BAT");
  66.         if (EXIST(dir))  return(strsave(dir));
  67.     }
  68.     return(NULL);
  69. }    
  70.         
  71. /* split path into array B[] with n elts */
  72. split(path,B)
  73. char *path;
  74. char *B[];
  75. {
  76.     int i=0; char *strtok(),*strsave(),*p, *tok;
  77.       p = strsave(path);
  78.       tok = strtok(p,";");
  79.       B[i++] = strsave(tok);
  80.       while(tok != NULL)
  81.       {
  82.       tok = strtok(NULL,";");
  83.      if (tok != NULL)
  84.       B[i++] = strsave(tok);
  85.       }
  86.       return(i);
  87. }
  88.  
  89. /* strsave - save string s somewhere; return address */
  90. char *strsave(s)
  91. char *s;
  92. {
  93.     char *malloc(), *p, *strcpy();
  94.     p = malloc(strlen(s)+1);    /* +1 to hold '\0' */
  95.     if (p==NULL) {
  96.         fprintf(stderr,"out of memory\n"); exit(1); }
  97.     return(strcpy(p, s));
  98. }
  99.  
  100. #define TIME_TYPE long
  101. #include <sys/stat.h>
  102. /* modtime - return file's modification time (0 for nonexistent files) */
  103. TIME_TYPE modtime(file)
  104. char *file;
  105. {
  106.     struct stat buf;
  107.     return(stat(file, &buf) >= 0 ? buf.st_mtime : 0);
  108. }
  109.